package model

import (
	"github.com/atcharles/wslt"
	jsoniter "github.com/json-iterator/go"
	"github.com/json-iterator/go/extra"
	"github.com/robfig/cron"

	"gitee.com/goframe/lt-collector/libs/orma"
)

var (
	json          jsoniter.API
	ModeWebsocket *wslt.WSocket
	ModeTask      *Task
	cleaners      = []Cleaner{
		new(OpenData),
	}
)

func Register() {
	extra.RegisterFuzzyDecoders()
	//extra.SupportPrivateFields()
	//extra.SetNamingStrategy(extra.LowerCaseWithUnderscores)
	json = jsoniter.ConfigFastest
	ModeWebsocket = wslt.New()
}

func init() {
	orma.Register(new(User))
	orma.Register(new(SysSetting))
	orma.Register(new(GameTypes))
	orma.Register(new(GameSourceTypes))
	orma.Register(new(GameLotteries))
	orma.Register(new(GameCollectSource))
	orma.Register(new(GameIssuePlan))
	orma.Register(new(GameIssueCheck))
	orma.Register(new(OpenData))
	orma.Register(new(GameSourceParamKcw))
	orma.Register(new(GameSourceParam168))
	orma.Register(new(ByCollector))
	orma.Register(new(TXCollector))
	orma.Register(new(GameSysTmp))
}

func NewTask() *Task {
	if ModeTask == nil {
		ModeTask = &Task{Cron: cron.New()}
		ModeTask.cleanRunner()

		ModeTask.Start()
	}
	return ModeTask
}

type (
	Cleaner interface {
		AutoClear()
	}

	Task struct {
		*cron.Cron
	}
)

func (t *Task) cleanRunner() {
	for _, cleaner := range cleaners {
		cleaner.AutoClear()
	}
}
